/**
 * Created by baichenguang on 2020/2/19.
 */
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
option = {
    title: {
        text: '温度变化',
        subtext: '2月'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['最高气温', '最低气温']
    },
    toolbox: {
        show: true,
        feature: {
            dataZoom: {
                yAxisIndex: 'none'
            },
            dataView: {readOnly: false},
            magicType: {type: ['line', 'bar']},
            restore: {},
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: []
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: '{value} °C'
        }
    },
    series: [
        {
            name: '最高气温',
            type: 'line',
            data: [],
            markPoint: {
                data: [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ]
            },
            markLine: {
                data: [
                    {type: 'average', name: '平均值'}
                ]
            }
        },
        {
            name: '最低气温',
            type: 'line',
            data: [],
            markPoint: {
                data: [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ]
            },
            markLine: {
                data: [
                    {type: 'average', name: '平均值'},
                    [{
                        symbol: 'none',
                        x: '90%',
                        yAxis: 'max'
                    }, {
                        symbol: 'circle',
                        label: {
                            position: 'start',
                            formatter: '最大值'
                        },
                        type: 'max',
                        name: '最高点'
                    }]
                ]
            }
        }
    ]
};
var getData = function (callback) {
    $.ajax({
        url: "/weather/weatherByMonth.json",
        dataType: "json",
        data: {year: 2020, month: 2},
        type: "GET",
        success: function (result) {
            var res_data = {};
            var res_max_data = [];
            var res_min_data = [];
            var xAxisData = [];
            //day tempera hun PM25 PM10 CO2 uv light voice form rain
            for (var i in result.objects) {
                var cur = result.objects[i];
                xAxisData.push(cur.day);
                res_max_data.push(cur.maxTempera);
                res_min_data.push(cur.minTempera);
            }
            res_data = {minData:res_min_data,maxData:res_max_data,xAxisData:xAxisData};
            callback(res_data)

        },
        error: function (result) {
            debugger;
        }
    });
};
if (option && typeof option === "object") {
    getData(function (data) {
        option.series[0].data = data.maxData;
        option.series[1].data = data.minData;
        option.xAxis.data = data.xAxisData;
        myChart.setOption(option, true);
    });
}